﻿using System.Composition.Hosting;
using System.Reflection;
using Windows.Storage;

namespace Unread.BackgroundTasks
{
    public static class Bootstrapper
    {
        public static void Run()
        {
            SetDefaultSettingsFileName();

            var container = Compose();
            ServicesFactory.Initialize(container);
        }

        private static CompositionHost Compose()
        {
            var configuration = new ContainerConfiguration()
                .WithAssembly(typeof(Unread.BackgroundTasks.Bootstrapper).GetTypeInfo().Assembly)
                .WithAssembly(typeof(Unread.Business.Services.Interfaces.IFeedAdder).GetTypeInfo().Assembly)
                .WithAssembly(typeof(Unread.Notifications.Services.Interfaces.ITileUpdater).GetTypeInfo().Assembly);

            return configuration.CreateContainer();
        }

        private static void SetDefaultSettingsFileName()
        {
            if (!ApplicationData.Current.RoamingSettings.Values.ContainsKey(Unread.Business.Data.Storage.Constants.DefaultSettingsFileName))
                ApplicationData.Current.RoamingSettings.Values[Unread.Business.Data.Storage.Constants.DefaultSettingsFileName] = "unread_feeds.xml";
        }
    }
}
